<?php

/**
 * Classe ctrlLogin  Control d'entrada
 * 
 * Projecte Silicon Party 2012
 * @version 1.0
 * @author  Jordi & Manel 08/04/2012
 * @copyright Creative commons (by-nc-nd) Reconeixement - NoComercial - CompartirIgual 
 */
include '../system/sessions.php';
require_once 'usuari.class.php';

class CtrlLogin {

    private $usuari;
    private $dni = "";

    public function __construct() {
        $this->usuari = new Usuari();
    }

    public function registra($usuari=null, $pass=null) {
        if (isset($usuari) && isset($pass)) {
            $usuaris = $this->usuari->listRecords("nick='".$usuari."' AND pass='".$pass."'");
            if (count($usuaris) > 0) {
                $_SESSION['dni'] = $this->dni = $usuaris[0]['dni'];
                $_SESSION['nick'] = $usuaris[0]['nick'];
                return true;
            }
        }
        return false;
    }

    public function logout() {
        session_destroy();
    }

    public function getRols() {
        $consulta = $this->usuari->getRols($this->dni);
        $rols = Array();
        foreach ($consulta as $rols)
            $rols[] = $rols['rols'];
        if (count($rols) > 0)
            $_SESSION['rols'] = $rols;
        return $rols;
    }

    public function getNick() {
        if (isset($_SESSION['nick']))
            return $_SESSION['nick'];
        return null;
    }

    public function getDni() {
        if (isset($_SESSION['dni']))
            return $_SESSION['dni'];
        return null;
    }

    public function esRegistrat() {
        return isset($_SESSION['dni']);
    }
}

?>
